package com.jianguozh.difs.support.api.superior;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jianguozh.difs.common.bean.R;
import com.jianguozh.difs.framework.base.BaseController;
import com.jianguozh.difs.framework.constant.SecurityConstant;
import com.jianguozh.difs.framework.validator.group.AddGroup;
import com.jianguozh.difs.framework.validator.group.UpdateGroup;
import com.jianguozh.difs.support.api.superior.dto.CateringDto;
import com.jianguozh.difs.support.api.superior.vo.CateringVo;
import com.jianguozh.difs.support.service.org.entity.Catering;
import com.jianguozh.difs.support.service.org.service.ICateringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 餐饮单位 前端控制器
 * </p>
 *
 * @author hanrs
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/catering")
public class CateringController extends BaseController {

    @Autowired
    ICateringService cateringService;

    /**
     * 由id获取对象
     */
    @GetMapping("/{id}")
    public R<Catering> get(@PathVariable String id) {
        return success(cateringService.getById(id));
    }

    /**
     * 新增
     */
    @PreAuthorize("hasAnyAuthority('" + SecurityConstant.AUTHORITY_TAG_SUPERIOR + "')")
    @PostMapping("")
    public R<Catering> add(@RequestBody @Validated(value = {AddGroup.class}) CateringDto cateringDto) {
        long count = cateringService.count(new LambdaQueryWrapper<Catering>().eq(Catering::getCateringName, cateringDto.getCateringName()));
        if (count > 0) {
            return failed("已经存在'" + cateringDto.getCateringName() + "'的餐饮单位.");
        }

        Catering entity = new Catering();
        BeanUtil.copyProperties(cateringDto, entity);
        boolean result = cateringService.save(entity);
        return result ? success(entity) : failed("操作失败.");
    }

    /**
     * 修改
     */
    @PreAuthorize("hasAnyAuthority('" + SecurityConstant.AUTHORITY_TAG_SUPERIOR + "')")
    @PutMapping("/{id}")
    public R<Catering> update(@RequestBody @Validated(value = {UpdateGroup.class}) CateringDto cateringDto) {
        long count = cateringService.count(new LambdaQueryWrapper<Catering>().eq(Catering::getCateringName, cateringDto.getCateringName()).ne(Catering::getId, cateringDto.getId()));
        if (count > 0) {
            return failed("已经存在'" + cateringDto.getCateringName() + "'的餐饮单位.");
        }

        Catering entity = new Catering();
        BeanUtil.copyProperties(cateringDto, entity);
        boolean result = cateringService.updateById(entity);
        return result ? success(entity) : failed("操作失败.");
    }

    /**
     * 批量删除
     */
    @PreAuthorize("hasAnyAuthority('" + SecurityConstant.AUTHORITY_TAG_SUPERIOR + "')")
    @DeleteMapping("/batch")
    public R<Boolean> deleteBatch(@RequestParam(value = "ids[]") String[] ids) {
        boolean result = cateringService.removeByIds(Arrays.asList(ids));
        return result ? success(true) : failed("操作失败.");
    }

    /**
     * 单个删除
     */
    @PreAuthorize("hasAnyAuthority('" + SecurityConstant.AUTHORITY_TAG_SUPERIOR + "')")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        boolean result = cateringService.removeById(id);
        return result ? success(true) : failed("操作失败.");
    }

    /**
     * 条件分页查询
     */
    @GetMapping("/page-query")
    public R<IPage<CateringVo>> pageQuery(HttpServletRequest request,
                                          @RequestParam(name = "cateringName", required = false) String cateringName) {

        LambdaQueryWrapper<Catering> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(cateringName))
            lambdaQueryWrapper.like(Catering::getCateringName, cateringName);

        IPage page = resolvePage(request);
        IPage<CateringVo> result = cateringService.page(page, lambdaQueryWrapper).convert(o -> {
            CateringVo vo = new CateringVo();
            BeanUtil.copyProperties(o, vo);
            vo.setDefaultManager(cateringService.getDefaultManager(vo.getId()));
            return vo;
        });
        return success(result);
    }

}
